package com.example.service;

import com.example.index.Product;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ElasticsearchService {


    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    // 创建索引
    public boolean createIndex() {
        return elasticsearchRestTemplate.indexOps(Product.class).create();
    }

    // 检查索引是否存在
    public boolean indexExists() {
        return elasticsearchRestTemplate.indexOps(Product.class).exists();
    }

    // 删除索引
    public boolean deleteIndex() {
        return elasticsearchRestTemplate.indexOps(Product.class).delete();
    }

    // 添加文档
    public Product saveProduct(Product product) {
        return elasticsearchRestTemplate.save(product);
    }

    // 根据ID查询
    public Product getProductById(String id) {
        return elasticsearchRestTemplate.get(id, Product.class);
    }

    // 更新文档
    public Product updateProduct(Product product) {
        return elasticsearchRestTemplate.save(product);
    }

    // 删除文档
    public void deleteProductById(String id) {
        elasticsearchRestTemplate.delete(id, Product.class);
    }


    // 全文搜索
    public List<Product> searchProducts(String keyword) {
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.multiMatchQuery(keyword, "name", "description"))
                .build();

        SearchHits<Product> searchHits = elasticsearchRestTemplate.search(searchQuery, Product.class);

        return searchHits.stream()
                .map(SearchHit::getContent)
                .collect(Collectors.toList());
    }

    // 条件查询 - 价格范围
    public List<Product> searchByPriceRange(double minPrice, double maxPrice) {
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.rangeQuery("price")
                        .from(minPrice)
                        .to(maxPrice))
                .build();

        SearchHits<Product> searchHits = elasticsearchRestTemplate.search(searchQuery, Product.class);

        return searchHits.stream()
                .map(SearchHit::getContent)
                .collect(Collectors.toList());
    }

    // 分页查询
    public List<Product> searchProductsWithPage(String keyword, int page, int size) {
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchQuery("name", keyword))
                .withPageable(PageRequest.of(page, size))
                .build();

        SearchHits<Product> searchHits = elasticsearchRestTemplate.search(searchQuery, Product.class);
        long totalHits = searchHits.getTotalHits();

        return searchHits.stream()
                .map(SearchHit::getContent)
                .collect(Collectors.toList());
    }
}